package com.sitech.portal.system.dao.ibatis;

import java.util.HashMap;
import java.util.List;

import com.sitech.ibnms.core.BaseIbnmsDao;
import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.portal.system.dao.ISysUserPrivilegeDao;
import com.sitech.portal.system.domain.TblSYS_USER_PRIVILEGE;

public class SysUserPrivilegeDaoImpl extends BaseIbnmsDao implements ISysUserPrivilegeDao {

	public IPage getAllUserPrivilegeList(PageForm pageForm) {
		String mySql = "";
		mySql = "  select log.name,log.login_id,unit.manage_unit_id,unit.manage_unit_name "
				+ " from tb_sys_user_privilege pri,tb_sys_loginmsg log,tb_sys_manage_unit unit "
				+ " where log.login_id = pri.login_id "
				+ " and pri.manage_unit_id = unit.manage_unit_id "
				+ " group by log.login_id ";
		return this.findPage(
				"findListForTB_SYS_USER_PRIVILEGEByPK", mySql, pageForm);
	}

	public List getUserPrivilegeListByLogin_id(String login_id) {
		String mysql = "";
		mysql = " select pri.privilege_id,log.name,log.login_id,unit.manage_unit_id,unit.manage_unit_name,pri.enable "
				+ " from tb_sys_user_privilege pri,tb_sys_loginmsg log,tb_sys_manage_unit unit "
				+ " where log.login_id = pri.login_id "
				+ " and pri.manage_unit_id = unit.manage_unit_id ";
		if (login_id != null && !"".equals(login_id)) {
			mysql += " and log.login_id like '%" + login_id + "%'";
		}
		return this.query("getUserPrivilegeListByLogin_id",
					mysql);
	}

	public IPage loadUserPrivilegeListByLogin_id(String login_id, PageForm pageForm) {
		String mysql = "";
		mysql = " select pri.privilege_id,log.name,log.login_id,unit.manage_unit_id,unit.manage_unit_name,pri.enable "
				+ " from tb_sys_user_privilege pri,tb_sys_loginmsg log,tb_sys_manage_unit unit "
				+ " where log.login_id = pri.login_id "
				+ " and pri.manage_unit_id = unit.manage_unit_id ";
		if (login_id != null && !"".equals(login_id)) {
			mysql += " and log.login_id like '%" + login_id + "%'";
		}
		mysql += "order by log.login_id asc";
		return this.findPage("getUserPrivilegeListByLogin_id",
				mysql, pageForm);
	}

	public void add(TblSYS_USER_PRIVILEGE tblSYS_USER_PRIVILEGE) {
		this.insert("insertForTB_SYS_USER_PRIVILEGE",
					tblSYS_USER_PRIVILEGE);
	}

	public List getUserPrivilegeListByPrivilege_Id(String privilege_id) {
		return this.query(
					"findUserPrivilegeListByPrivilege_id", privilege_id);
	}

	public void deleteUserPrivilegeByPrivilege_id(
			TblSYS_USER_PRIVILEGE tblSYS_USER_PRIVILEGE) {
		this.delete("deleteForTB_SYS_USER_PRIVILEGE",
					tblSYS_USER_PRIVILEGE);
	}

	public void deleteUserPrivilegeByLogin_id(String login_id) {
		this.delete("deleteForTblSYS_USER_PRIVILEGEByLogin_id",
					login_id);
	}

	public String getUserPrivilegeStrByLogin_id(String login_id) {
		String privilegeStr = "";
		if (login_id != null && !"".equals(login_id)) {
			List privilegeList = getUserPrivilegeListByLogin_id(login_id);
			for (int i = 0; i < privilegeList.size(); i++) {
				HashMap map = (HashMap) privilegeList.get(i);
				privilegeStr += map.get("MANAGE_UNIT_ID") + ",";
			}
		}
		return privilegeStr;
	}
}
